package com.zhifei.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.DateUtils;
import com.zhifei.common.utils.DispatcherUtils;
import com.zhifei.system.domain.DingUserArchivesEntity;
import com.zhifei.system.domain.DingUserEntity;
import com.zhifei.system.domain.vo.PersonProjectUserArchivesVo;
import com.zhifei.system.domain.vo.UserArchivesSummaryVo;
import com.zhifei.system.mapper.DingUserArchivesMapper;
import com.zhifei.system.service.DingUserArchivesService;
import com.zhifei.system.util.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DingUserArchivesServiceImpl extends ServiceImpl<DingUserArchivesMapper, DingUserArchivesEntity> implements DingUserArchivesService {

    @Autowired
    private DingUserArchivesMapper dingUserArchivesMapper;

    @Override
    public void generateArchives(Date firstDayOfNowMonth, Date lastDayOfNowMonth) {
        List<DingUserArchivesEntity> dingUserArchivesEntityList = new ArrayList<>();
        List<DingUserEntity> dingUserEntityList = dingUserArchivesMapper.listAllProjectUser(1);
        for(DingUserEntity dingUserEntity : dingUserEntityList){
            DingUserArchivesEntity dingUserArchivesEntity = new DingUserArchivesEntity();
            BeanUtils.copyProperties(dingUserEntity,dingUserArchivesEntity);
            dingUserArchivesEntity.setMonth(firstDayOfNowMonth);
            dingUserArchivesEntityList.add(dingUserArchivesEntity);
        }
        List<DingUserEntity> dingUserEntities = dingUserArchivesMapper.listAllProjectUser(2);
        for(DingUserEntity dingUserEntity : dingUserEntities){
            DingUserArchivesEntity dingUserArchivesEntity = new DingUserArchivesEntity();
            BeanUtils.copyProperties(dingUserEntity,dingUserArchivesEntity);
            dingUserArchivesEntity.setMonth(firstDayOfNowMonth);
            dingUserArchivesEntityList.add(dingUserArchivesEntity);
        }
        this.saveBatch(dingUserArchivesEntityList);
    }

    @Override
    public PageUtils pageUserArchives(Map<String, Object> params) {
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        String deptId = (String) params.get("deptId");
        Integer userStatus = DispatcherUtils.parseInteger(params.get("userStatus"));
        String monthStartDate = (String) params.get("monthStartDate");
        String monthEndDate = (String) params.get("monthEndDate");
        List<PersonProjectUserArchivesVo> personProjectUserArchivesVoList = dingUserArchivesMapper.pageUserArchives(page,deptId,userStatus,monthStartDate,monthEndDate);
        page.setRecords(personProjectUserArchivesVoList);
        return new PageUtils(page);
    }

    @Override
    public UserArchivesSummaryVo getUserArchivesSummary(Map<String, Object> params) {
        UserArchivesSummaryVo userArchivesSummaryVo = new UserArchivesSummaryVo();
        String deptId = (String) params.get("deptId");
        Integer userStatus = DispatcherUtils.parseInteger(params.get("userStatus"));
        String monthStartDate = (String) params.get("monthStartDate");
        String monthEndDate = (String) params.get("monthEndDate");
        List<DingUserArchivesEntity> dingUserArchivesEntityList = dingUserArchivesMapper.listUserArchives(deptId,userStatus,monthStartDate,monthEndDate);
        userArchivesSummaryVo.setUserCount(dingUserArchivesEntityList.size());
        BigDecimal userCostSummary = BigDecimal.ZERO;
        for(DingUserArchivesEntity dingUserArchivesEntity : dingUserArchivesEntityList){
            if(dingUserArchivesEntity.getUserStatus()==1){//在项直接加上月薪
                userCostSummary = userCostSummary.add(dingUserArchivesEntity.getMonthPay());
            }else if(dingUserArchivesEntity.getUserStatus()==2 || dingUserArchivesEntity.getUserStatus()==3){//离职或在职出项加上（天数比例*月薪）
                long percent = DateUtils.getTimePercent(dingUserArchivesEntity.getLeaveProjectDate());
                BigDecimal userCost = new BigDecimal(percent).multiply(dingUserArchivesEntity.getMonthPay()).divide(new BigDecimal(100),2, RoundingMode.HALF_UP);
                userCostSummary = userCostSummary.add(userCost);
            }
        }
        userArchivesSummaryVo.setUserCostSummary(userCostSummary);
        return userArchivesSummaryVo;
    }
}
